/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.clientserver;

/**
 *
 * @author Mr.Sam
 */
import fr.ove.utils.Connection;
import fr.ove.utils.ConnectionHandler;
import fr.ove.openmath.mathematica.ResponseReader;
import java.io.IOException;

/**
* A ConnectionHandler implementation.<BR>
* This connection handler is used with a server which connects to an another one.
* The objects sent from a connected client are sent to the server the server whose
* instance deals the conections is connected.
*/
public class PipeConnectionHandler implements ConnectionHandler {    
    String hostName;
    int portNumber;
    /**
    * The constructor.
    * @param hostName the name of the host the instance connects to.
    * @param portNumber the port number through the instance is connected.
    */
    public PipeConnectionHandler(String hostName, int portNumber) {
        this.portNumber = portNumber;
        this.hostName = hostName;
        
    }
    
    // #################################
    // ### Interfaces Implementation ###
    // #################################    

    // ConnectionHandler interface

    /**
    * Handles the specified connection.
    * @param connection the connection to handle.
    */
    public void handleConnection(Connection connection) {
        Connection serverConnection = new Connection(hostName, portNumber);
        try {
            System.out.println("on open une connection");
            serverConnection.open();
        }
        catch (IOException ioe) {
            System.out.println("Failed to open a connection to the server");
            ioe.printStackTrace();
        }
        new Link(connection, serverConnection);
    }
    
    /**
    *
    */
    private class Link implements Runnable {
        private Connection clientConnection;
        private Connection serverConnection;
        private ResponseReader reader = new ResponseReader();
        
        public Link(Connection clientConnection, Connection serverConnection) {
            this.clientConnection = clientConnection;
            this.serverConnection = serverConnection;
            
            new Thread(this).start();
        }
        // Runnable interface

        /**
        *
        */
        public void run() {
            String object = null;
            
            while (true) {
                try {Thread.sleep(100);} catch (Exception e) {e.printStackTrace();}
                if (clientConnection != null) {
                    // Read the object sent by the client.
                    object = reader.read(clientConnection.getInputStream());
                    // if we got it, send it to the server
                    if (object != null) {
                        try {
                            serverConnection.getOutputStream().write(object.getBytes());
                            // Wait for the response from the server
                            object = reader.read(serverConnection.getInputStream());
                            // if we got something, send it to the client
                            if (object != null) {
                                try {
                                    clientConnection.getOutputStream().write(object.getBytes());
                                }
                                catch (IOException ioe) {
                                    System.out.println("Failed to send back response to the client");
                                    ioe.printStackTrace();
                                }
                            }
                        }
                        catch (IOException ioe) {
                            System.out.println("Failed to pipe the object to the server");
                            ioe.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}